function str2Json(str) {
	try {
		return JSON.parse(str);
	} catch (e) {

	}
}

function resJsonErr(description) {
	return {
		status: "error",
		description: description
	}
}

function resJsonOk(data) {
	var result = {
		status: "ok",
		description: "ok"
	};
	if (data) {
		result.data = data;
	}
	return result;
}

function makePost(url, bodyStr, callback) {
	var http = require('http');
	var URL = require('url');

	var detail = URL.parse(url, true);
	var opt = {
		host: detail.hostname,
		port: detail.port,
		path: detail.pathname,
		method: 'POST',
	};

	var req = http.request(opt, function(res) {
		res.setEncoding('utf8');
		var data = '';
		res.on('data', function(chunk) {
			data += chunk;
		});
		res.on('end', function() {
			callback(null, data);
		});
	});
	req.on('error', function(error) {
		console.log("makePost error:" + url);
		callback(error);
	});
	req.write(bodyStr);
	req.end();
}

module.exports = {
	str2Json: str2Json,
	resJsonErr: resJsonErr,
	resJsonOk: resJsonOk,
	makePost: makePost
}